<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Goods extends Allow
{
    public function getindex()
    {
        // 获取分类id
        $goods=Db::table("pro_goods")->select();
        // var_dump($goods);
        for($i=0;$i<count($goods);$i++){
            $cate_id = $goods[$i]['cate_id'];
            $cate_id = explode(",",$cate_id);
            $cate_id = array_pop($cate_id);
            $cate_id = Db::table("cates")->where('id',"{$cate_id}")->select();
            $cate_name = $cate_id['0']['name'];
            $cate_path = $cate_id['0']['path'];
            // 获取商品品牌
            $brand_id = $goods[$i]['brand_id'];
            $brand = Db::table("brand")->where('id',"{$brand_id}")->select();
            foreach($brand as $v){
                // var_dump($brand);exit;
                $goods[$i]['brand_id'] =$v['brand'];
            }
            // var_dump($cate_name);
            // var_dump($cate_path);exit;
            //判断是几级分类
            $con = substr_count($cate_path,',');
            for($j=0;$j<$con;$j++){
                $cate_name = '--|'.$cate_name;
            }
            // var_dump($cate_name);exit;
            $goods[$i]['cate_id'] = $cate_name;
        }
        // var_dump($goods);exit;
    	// 加载商品列表模板
    	return $this->fetch("Goods/index",['goods'=>$goods]);   
    }

    // //添加
    public function getadd(){
        //调用分割函数
        $cate=$this->getcates();
        // var_dump($cate);exit;
        // //加载商品列表模板
        return $this->fetch("Goods/add",['cate'=>$cate]);
    }

    //执行添加
    public function postinsert(){
        // echo '1';
        $request=request();
        //获取添加数据
        $data = $request->param();
        // $data1 = array_pop($data);
        // var_dump($data);
        $cate_id = explode(",",$data['cate_id']);
        $cate_id = array_pop($cate_id);
        $cate_id = Db::table("cates")->where('id',"{$cate_id}")->select();
        $cate_name = $cate_id['0']['name'];
        $cate_path = $cate_id['0']['path'];
        // var_dump($cate_name);
        // var_dump($cate_path);exit;
        //判断是几级分类
        $con = substr_count($cate_path,',');
        for($j=0;$j<$con;$j++){
            $cate_name = '--|'.$cate_name;
        }
        $data['cate_name'] = $cate_name;
        // var_dump($data['cate_id']);exit;
        $d=$data['cate_id'];
        if(isset(explode(",",$d)[3]))$data['cates_id']= explode(",",$d)[3];
        $data['cate_id']= explode(",",$data['cate_id'])[1];
        // var_dump($data);exit;

        $file = $request->file('pic');
        //设置上传规则
        $result = $this->validate(['file1'=>$file],['file1'=>'require|image'],['file1.require'=>'上传文件不能为空','file1.image'=>'上传文件必须是图像类型']);
        if(true !==$result){
            $this->error($result,"/admingoods/add");
        }
        //移动到指定目录
        $info = $file->move(ROOT_PATH.'public'.DS.'uploads');
        
        $pic = $info->getSavename();

        // var_dump($pic);exit;
        $data['pic']=$pic;
        unset($data['action']);
        // var_dump($data);exit;
        if($data['name']&&$data['cate_id']&&$data['descr']&&$data['num']&&$data['price']&&$data['cate_name']){
            //将数据写入数据库
            if(Db::table("pro_goods")->insert($data)){
                $this->success("商品添加成功","/admingoods/index");
            }else{
                $this->error("商品添加失败","/admingoods/add");
            }
        }else{$this->error("请填写完整商品信息,请重新添加");}
    }
    // //删除
    public function getdelete(){
        // echo '111';exit;
        $request=request();
        //获取id
        $id=$request->param('id');
        $info = Db::table('pro_goods')->where('id',"{$id}")->select();
        $pic = $info['0']['pic'];
        // var_dump($pic);exit;
        if(Db::table('pro_goods')->where('id',"{$id}")->delete()){
            unlink('./uploads/'.$pic);
            $this->success("删除成功","/admingoods/index");
        }else{
            $this->error("删除失败","/admingoods/index");
        }
    }

    // //编辑
    public function getedit(){
        // echo '1';exit;
        //调用分割函数
        $cate=$this->getcates();

        //创建请求
        $request=request();
        //获取id
        $id=$request->param('id');
        // var_dump($id);
        $result = Db::query("select * from pro_goods where id={$id}");
        $res = $result['0'];
        // var_dump($res);exit;
        //查询当前商品的品牌信息
        $brand_id = $res['brand_id'];
        $brand_name = Db::query("select * from brand where id={$brand_id}");
        // var_dump($brand_name);exit;
        $res['brand_name'] = $brand_name['0']['brand'];
        // var_dump($res);exit;
        //查询全部品牌信息
        $brand = Db::query("select * from brand");
        $this->assign([
            'res'  => $res,
            'cate' => $cate,
            'brand' => $brand
        ]);
        // var_dump($res);exit;
        // var_dump($cate);exit;
        return $this->fetch("Goods/edit");
    }
    //执行编辑操作
    public  function postupdate(){
        $request=request();
        //获取id
        $data=$request->param();
   

        $d=$data['cate_id'];
        if(isset(explode(",",$d)[3]))$data['cates_id']= explode(",",$d)[3];
        $data['cate_id']= explode(",",$data['cate_id']);
        // $data = Db::table('pro_goods')->where('id',"{$data['id']}")->select();
        // var_dump($data['oldpic']);exit;
        
        // 获取上传文件
        $file = $request->file('pic');
        // var_dump($file);exit;
        
        //设置上传规则
        $result = $this->validate(['file1'=>$file],['file1'=>'require|image'],['file1.require'=>'上传文件不能为空','file1.image'=>'上传文件必须是图像类型']);
        if(true !==$result){
            $this->error($result);
        }
        //删除与原来的图片
        unlink('./uploads/'.$data['oldpic']);
        //移动到指定目录
        $info = $file->move(ROOT_PATH.'public'.DS.'uploads');
        
        $pic = $info->getSavename();
        // str_replace("\\","/",$pic);
        $pic = $pic;
        // str_replace("\\","/",$pic);
        // var_dump($pic);exit;
        $data['pic']=$pic;

        //删除多余的字段
        unset($data['action']);
        unset($data['oldpic']);

        
        // var_dump($data);exit;
        $id = $data['id'];
        $name = $data['name'];
        $cate_id = $data['cate_id'];
        $descr = $data['descr'];
        $num = $data['num'];
        $price = $data['price'];
        $pic= $data['pic'];
        // $cate_name = $data['cate_name'];
        unset($data['id']);

        $cate_id = explode(",",$d);
        $cate_id = array_pop($cate_id);
        $cate_id = Db::table("cates")->where('id',"{$cate_id}")->select();
        $cate_name = $cate_id['0']['name'];
        $cate_path = $cate_id['0']['path'];
        // var_dump($cate_name);
        // var_dump($cate_path);exit;
        //判断是几级分类
        $con = substr_count($cate_path,',');
        for($j=0;$j<$con;$j++){
            $cate_name = '--|'.$cate_name;
        }
        $data['cate_name'] = $cate_name;
        // var_dump($data);exit;
        if($data['name']&&$data['cate_id']&&$data['descr']&&$data['num']&&$data['price']&&$data['cate_name']){
            //执行更新
            if(Db::table('pro_goods')->where('id',$id)->update($data)){
                $this->success("更新成功","/admingoods/index");
            }else{
                $this->error("/admingoods/index");
            }
        }else{$this->error("请填写完整商品信息,请重新编辑");}
    }



    //分割列表
    public function getcates(){
        $cate=Db::query("select *,concat(path,',',id) as paths from cates order by paths");
        //遍历
        foreach($cate as $key=>$value){
            //获取path
            $path=$value['path'];
            //转换为数组
            $arr=explode(",",$path);
            //获取逗号个数
            $len=count($arr)-1;
            //添加分割符
            $cate[$key]['name']=str_repeat("--|",$len).$value['name'];
        }
        return $cate;
    }
    //变换状态
    public  function getstatus(){
        $request=request();
        //获取id
        $data=$request->only(['id','status']);
        $id = $data['id'];
        $status = $data['status'];
        //判断连接状态
        if($status=='0'){
            $status='1';
        }else{
            $status='0';
        }
        // var_dump($id);
        // var_dump($status);
        if(Db::execute("update pro_goods set status='{$status}' where id='{$id}'")){
            $this->redirect("/admingoods/index");
        }else{
            $this->error("/admingoods/index");
        }
    }
}
